﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.IO;
using System.Collections.Generic;
using WebCamView.Web.Code;

namespace WebCamView.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        private static byte[] _lastImage;

        private static ImageData _lastImageMessage;

        private static CamRepository repository = new CamRepository();
       
        [OperationContract]
        public string DoWork()
        {
            // Add your operation implementation here
            return "doWork";
        }

        [OperationContract]
        public void SaveImage(byte[] imageSource)
        {
            _lastImage = imageSource;
        }

        [OperationContract]
        public byte[] LoadImage()
        {
            return _lastImage;
        }

        [OperationContract(IsOneWay = true)]
        public void SaveImageMessage(ImageData image)
        {
            _lastImageMessage = image;
        }

        [OperationContract]
        public ImageData LoadImageMessage()
        {
            return _lastImageMessage;
        }

        /// <summary>
        /// join the translation
        /// </summary>
        /// <returns>array of the meta infos of current cams</returns>
        [OperationContract]
        public MetaCamInfo [] Join(MetaCamInfo myinfo)
        {
            if (!repository.IsExist(myinfo))
                repository.RegisterCam(myinfo);
            return repository.GetCameras();
        }

        /// <summary>
        /// get list of cameras
        /// </summary>
        /// <returns>list of cameras</returns>
        [OperationContract]
        public MetaCamInfo [] GetFullList()
        {
            return repository.GetCameras();
        }

        [OperationContract]
        public ImageData GetImage(MetaCamInfo metaInfo)
        {
            return repository.GetImage(metaInfo);
        }

        [OperationContract(IsOneWay=true)]
        public void SendImage(MetaCamInfo metaInfo, ImageData image)
        {
            repository.SaveImage(metaInfo, image);
        }
    }
}
